package com.shujia.wyh;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/*
    使用配置存储连接信息
 */
public class JDBCDemo3 {
    public static void main(String[] args) {

        Connection conn = null;
        try {

            //读取配置文件
            Properties properties = new Properties();
            properties.load(new BufferedReader(new FileReader("mysql/src/main/java/com/shujia/wyh/utils/mysql.properties")));
            //获取数据库的驱动包位置
            String dbVersion = properties.getProperty("dbVersion");
            //获取数据库的连接
            String url = properties.getProperty("url");
            //获取登录数据库的用户名
            String user = properties.getProperty("user");
            //获取登录数据库的密码
            String password = properties.getProperty("password");


            Class.forName(dbVersion);

            conn = DriverManager.getConnection(url, user, password);

            System.out.println("成功与数据库建立连接！！" + conn);

            //执行sql
            //解析结果

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
